package xx.rabbitmq.chat.config;


import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import xx.rabbitmq.chat.constant.ConstantUtils;
import xx.rabbitmq.chat.entity.ChatMsg;
import xx.rabbitmq.chat.websocket.WebSocketServer;

import java.io.IOException;
import java.util.Map;

@Slf4j
@Component
@RabbitListener(queues = ConstantUtils.FANOUT_QUEUE_MSG)
public class FanoutReceiverMsg {
    @RabbitHandler
    public void process(String string) throws IOException {
        if (string != null) {
            log.info("MQ 接收到消息：{}",string);
            ChatMsg chatMsg = JSON.parseObject(string, ChatMsg.class);
            WebSocketServer.oneToOne(chatMsg.getReceiver(), chatMsg);
        }
    }
}
